package gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;

import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;

import componenti.LeggeDistribuzione;

public class PCPanel extends JPanel {

	public final static int PRODUTTORE = 0, CONSUMATORE = 1;
	private JPanel upPanel;
	private JPanel downPanel;
	private JLabel label;
	private JComboBox comboBox;
	private int TYPE;

	public PCPanel(int TYPE) {

		this.TYPE = TYPE;
		this.setLayout(new GridBagLayout());

		initUpPanel();
		initDownPanel();

	}

	private void initUpPanel() {
		upPanel = new JPanel(new GridBagLayout());
		this.add(upPanel, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						10, 10, 5, 2), 0, 0));
		switch (TYPE) {
		case PRODUTTORE:
			label = new JLabel("PRODUTTORE");
			break;

		case CONSUMATORE:
			label = new JLabel("CONSUMATORE");
			break;
		}

		comboBox = new JComboBox(LeggeDistribuzione.Tipi.values());

		upPanel.add(label, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						10, 10, 5, 2), 0, 0));

		upPanel.add(comboBox, new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						10, 10, 5, 2), 0, 0));

	}

	private void initDownPanel() {
		downPanel = new JPanel(new GridBagLayout());
		this.add(downPanel, new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						10, 10, 5, 2), 0, 0));

		JLabel lambda = new JLabel("Lambda");
		JFormattedTextField lambdaTx = new JFormattedTextField(
				NumberFormat.getIntegerInstance());

		downPanel.add(lambda, new GridBagConstraints(0, 2, 1, 1, 1.0, 1.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						10, 10, 5, 2), 0, 0));
		downPanel.add(lambdaTx, new GridBagConstraints(1, 2, 1, 1, 1.0, 1.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						10, 10, 5, 2), 0, 0));

		comboBox.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JLabel lambda = new JLabel("Lambda");
				JFormattedTextField lambdaTx = new JFormattedTextField(
						NumberFormat.getIntegerInstance());
				lambdaTx.setColumns(4);
				JLabel numeroFunzioni = new JLabel("Numero Funzioni");
				JFormattedTextField numeroFunzioniTx = new JFormattedTextField(
						NumberFormat.getIntegerInstance());
				numeroFunzioniTx.setColumns(4);

				switch ((LeggeDistribuzione.Tipi) comboBox.getSelectedItem()) {

				case ESPONENZIALE:
					downPanel.removeAll();
					downPanel.add(lambda, new GridBagConstraints(0, 2, 1, 1,
							1.0, 1.0, GridBagConstraints.CENTER,
							GridBagConstraints.BOTH, new Insets(10, 10, 5, 2),
							0, 0));
					downPanel.add(lambdaTx, new GridBagConstraints(1, 2, 1, 1,
							1.0, 1.0, GridBagConstraints.CENTER,
							GridBagConstraints.BOTH, new Insets(10, 10, 5, 2),
							0, 0));
					PCPanel.this.getParent().getParent().getParent().getParent().getParent().repaint();
					break;

				case ERLANG:
					downPanel.removeAll();
					downPanel.add(lambda, new GridBagConstraints(0, 2, 1, 1,
							1.0, 1.0, GridBagConstraints.CENTER,
							GridBagConstraints.BOTH, new Insets(10, 10, 5, 2),
							0, 0));
					downPanel.add(lambdaTx, new GridBagConstraints(1, 2, 1, 1,
							1.0, 1.0, GridBagConstraints.CENTER,
							GridBagConstraints.BOTH, new Insets(10, 10, 5, 2),
							0, 0));

					downPanel.add(numeroFunzioni, new GridBagConstraints(0, 3,
							1, 1, 1.0, 1.0, GridBagConstraints.CENTER,
							GridBagConstraints.BOTH, new Insets(10, 10, 5, 2),
							0, 0));
					downPanel.add(numeroFunzioniTx, new GridBagConstraints(1,
							3, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER,
							GridBagConstraints.BOTH, new Insets(10, 10, 5, 2),
							0, 0));
//					System.out.println(PCPanel.this.getParent().getParent().getParent().getParent().getParent().getClass());
					((StartScreen)PCPanel.this.getParent().getParent().getParent().getParent().getParent()).getCenterPane().repaint();
					break;

				case IPERESPONENZIALE:
					downPanel.removeAll();
					downPanel.add(lambda, new GridBagConstraints(0, 2, 1, 1,
							1.0, 1.0, GridBagConstraints.CENTER,
							GridBagConstraints.BOTH, new Insets(10, 10, 5, 2),
							0, 0));
					downPanel.add(lambdaTx, new GridBagConstraints(0, 3, 1, 1,
							1.0, 1.0, GridBagConstraints.CENTER,
							GridBagConstraints.BOTH, new Insets(10, 10, 5, 2),
							0, 0));
					downPanel.add(numeroFunzioni, new GridBagConstraints(0, 3,
							1, 1, 1.0, 1.0, GridBagConstraints.CENTER,
							GridBagConstraints.BOTH, new Insets(10, 10, 5, 2),
							0, 0));
					downPanel.add(numeroFunzioniTx, new GridBagConstraints(1,
							3, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER,
							GridBagConstraints.BOTH, new Insets(10, 10, 5, 2),
							0, 0));
					break;
				default:
					downPanel.removeAll();
					downPanel.add(lambda, new GridBagConstraints(0, 2, 1, 1,
							1.0, 1.0, GridBagConstraints.CENTER,
							GridBagConstraints.BOTH, new Insets(10, 10, 5, 2),
							0, 0));
					downPanel.add(lambdaTx, new GridBagConstraints(0, 3, 1, 1,
							1.0, 1.0, GridBagConstraints.CENTER,
							GridBagConstraints.BOTH, new Insets(10, 10, 5, 2),
							0, 0));
					break;
				}
			}
		});

	}

}
